package com.fancy.learncenter.ui.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.support.v7.widget.CardView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.fancy.learncenter.R;
import com.fancy.learncenter.common.MyApplication;

/**
 * Created by Hyy on 2017/2/14.
 */

public class PayPopupWindow extends BasePopupWindow {
    View popopView;

    View locationView;

    Activity mContext;
    LinearLayout wxPay;
    LinearLayout aliPay;

    ImageView close;
    private CardView ll_popup;
    private AnimatorSet animSet;
    private ObjectAnimator animatorAlpha;
    private ObjectAnimator animatorScalex;


    public PayPopupWindow(Activity mContext, View locationView) {
        super(mContext);
        this.mContext = mContext;
        this.locationView = locationView;
        popopView = LayoutInflater.from(MyApplication.context).inflate(R.layout.popupwindow_pay, (ViewGroup) locationView, false);
        ll_popup = (CardView) popopView.findViewById(R.id.ll_popup);

        wxPay = (LinearLayout) popopView.findViewById(R.id.wx_pay);

        aliPay = (LinearLayout) popopView.findViewById(R.id.ali_pay);

        close = (ImageView) popopView.findViewById(R.id.close);

        initPopupWionw();

    }

    private void initPopupWionw() {

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);

        setOutsideTouchable(true);

        setContentView(popopView);

        popopView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        ll_popup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        wxPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payCallBack.wxPay();
            }
        });
        aliPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payCallBack.aliPay();
            }
        });

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    @Override
    public void showPopupWindow() {
        super.showPopupWindow();

        animatorAlpha = ObjectAnimator.ofFloat(ll_popup, "alpha", 0.8f, 1f);
        int animateHeight = ll_popup.getHeight();
        if (animateHeight <= 0) {
            int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            ll_popup.measure(w, h);
            animateHeight = ll_popup.getMeasuredHeight();
        }

        animatorScalex = ObjectAnimator.ofFloat(ll_popup, "translationY", -animateHeight, 0);
        animSet = new AnimatorSet();
        animSet.play(animatorAlpha).with(animatorScalex);
        animSet.setDuration(500);
        animSet.setInterpolator(new AccelerateDecelerateInterpolator());
        animSet.start();

        showAtLocation(locationView, Gravity.CENTER, 0, 0);
    }

    public PayCallBack getPayCallBack() {
        return payCallBack;
    }

    public void setPayCallBack(PayCallBack payCallBack) {
        this.payCallBack = payCallBack;
    }

    private PayCallBack payCallBack;

    public interface PayCallBack {
        abstract void aliPay();

        abstract void wxPay();
    }
}
